/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eng.scc.core.parts;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author Rafael
 */
public class ColorSelector extends JPanel
{
    private Color bgColor = getBackground();
    
    public ColorSelector()
    {
        setMinimumSize( new Dimension(22, 22));
        setPreferredSize( new Dimension(22, 22));
        
        addMouseListener( new MouseAdapter()
        {

            @Override
            public void mouseClicked( MouseEvent e )
            {
                bgColor = JColorChooser.showDialog( ColorSelector.this,
                                                          "Selecione uma cor",
                                                          bgColor);
                
                revalidate();
                repaint();
            }
        } );
    }

    public static void main( String[] args )
    {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setSize( 600, 600 );
        ColorSelector cs = new ColorSelector();

        frame.add( cs );
        frame.setVisible( true );
    }

    @Override
    protected void paintComponent( Graphics g )
    
    {
        super.paintComponent( g );
        Graphics2D g2d = ( Graphics2D ) g.create();
        g2d.setColor( Color.BLACK );
        g2d.drawOval( 0, 0, 20, 20 );
        
        g2d.setColor( bgColor );
        g2d.fillOval( 0, 0, 20, 20 );
        g2d.dispose();
    }

    public void setColor( String color )
    {
        if ( color != null && !color.isEmpty() )
        {
            bgColor = new Color( Integer.valueOf( color ) );
            revalidate();
            repaint();
        }
    }
    
    public String getColor()
    {
        return String.valueOf( bgColor.getRGB() );
    }
}
